package com.aplink.generic.widgets;

import java.text.ParseException;

import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.database.SqlMethods;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.ContentData;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.textview.DisplayStyle;
import com.aplink.generic.pojo.controlspec.textview.TextViewSpec;
import com.aplink.generic.util.DateTimeUtils;
import com.aplink.generic.util.LogUtil;
import com.aplink.generic.xmlparser.ContentDataParser;

public class ApLinkTextview extends ApLinkBaseView {

    private static final long serialVersionUID = -5582163833695876492L;
    private ContentData mContentData;
    private final TextViewSpec mLabelSpec;
    private final TextView mTextView;

    public ApLinkTextview(final GenericFragment dataEntryActivity,
            final ControlSpec controlSpec, final View view) {
        super(dataEntryActivity, controlSpec, view);
        this.mTextView = (TextView) view;
        this.mLabelSpec = (TextViewSpec) controlSpec;

    }

    @Override
    public void clear() {
        this.mTextView.setText("");
    }

    @Override
    public GenericAttribute getAttribute() {
        final String name = this.mLabelSpec.getFullColumnName();
        final ObjPropInfo objPropInfo = getGenericFragment().getContentData()
                .get(name);
        final DisplayStyle displayType = this.mLabelSpec.getDisplayStyle();
        String value = this.mTextView.getText().toString();
        if (displayType.equals(DisplayStyle.DATETIME)
                || displayType.equals(DisplayStyle.CURRENT_DATETIME)) {
            try {
                final long miliseconds = DateTimeUtils.parse(value);
                value = String.valueOf(miliseconds / 1000L);
            } catch (final ParseException e) {
                LogUtil.error(e.getMessage(), "Get attribute from textview",
                        getClass());
            }
        }
        return GenericAttribute.obtain(value, objPropInfo);
    }

    @Override
    public GenericDictionary<String, GenericAttribute> getListAttributes() {
        return null;
    }

    @Override
    public TextView getView() {
        return this.mTextView;
    }

    @Override
    protected void initViewProperties() {
        final boolean isVisibility = this.mLabelSpec.isVisibility();
        this.mTextView.setMovementMethod(new ScrollingMovementMethod());
        if (!isVisibility) {
            this.mTextView.setVisibility(View.GONE);
        }
        this.mTextView.setTag(this.mLabelSpec);
    }

    @Override
    protected void loadBindingObject() {
        if (TextUtils.isEmpty(this.mLabelSpec.getContentData())) {
            final String dataObj = this.mLabelSpec.getDataObject();
            String content = null;
            final String mappingName = this.mLabelSpec.getMappingName();
            final DisplayStyle displayStyle = this.mLabelSpec.getDisplayStyle();
            final String fullColumnName = dataObj + "." + mappingName;
            final GenericObject initValue = getGenericFragment()
                    .getInitValues();
            if (dataObj != "") {
                try {
                    if (displayStyle.equals(DisplayStyle.DATETIME)
                            || displayStyle
                                    .equals(DisplayStyle.CURRENT_DATETIME)) {
                        final String longValue = getHashPassedValues().get(
                                fullColumnName);
                        if ((longValue != null) && !longValue.equals("0")) {
                            final Double milis = Double.parseDouble(longValue);
                            final Long milliseconds = milis.longValue();
                            content = DateTimeUtils.getDateTime(milliseconds,
                                    this.mLabelSpec.getDisplayFormat());
                        } else {
                            final long milliseconds = DateTimeUtils
                                    .getCurrentDateTime(true);
                            content = DateTimeUtils.getDateTime(true,
                                    milliseconds,
                                    this.mLabelSpec.getDisplayFormat());
                        }
                    } else if ((initValue != null)
                            && initValue.containsKey(fullColumnName)) {
                        content = initValue.get(fullColumnName).getValue();
                    } else {
                        content = getHashPassedValues().get(fullColumnName);
                    }
                } catch (final NumberFormatException e) {
                    LogUtil.error(e.getMessage(),
                            "loadPassedDataValueIntoView", getClass());
                } catch (final ParseException e) {
                    e.printStackTrace();
                }
            }
            this.mTextView.setText(content == null ? "" : content);
        } else {
            try {
                loadDatas();
            } catch (final ParseException e) {
                e.printStackTrace();
            }
        }
    }

    private void loadDatas() throws ParseException {
        this.mContentData = ContentDataParser.getInstance().parseIntoObjProp(
                this.mLabelSpec.getContentDataID());
        if ((this.mContentData != null) && !this.mContentData.isEmpty()) {
            final String whereCondition = this.mLabelSpec.getFilterBy();

            GenericObject genericObject = null;
            String whereClause = null;
            if (TextUtils.isEmpty(whereCondition)) {
                final String value = getHashPassedValues().get(whereCondition);
                final SqlMethods methods = SqlMethods.getInstance().equal(
                        whereCondition, value);
                whereClause = methods.toString();
            }
            genericObject = getDatabaseGet()
                    .get(this.mContentData, whereClause);
            if (genericObject == null) {

            } else {
                final String dataObj = this.mLabelSpec.getDataObject();
                final String mappingName = this.mLabelSpec.getMappingName();
                final String fullColumnName = dataObj + "." + mappingName;
                String content = genericObject.getValue(fullColumnName);
                final DisplayStyle displayStyle = this.mLabelSpec
                        .getDisplayStyle();
                if (displayStyle.equals(DisplayStyle.DATETIME)
                        || displayStyle.equals(DisplayStyle.CURRENT_DATETIME)) {
                    final String longValue = genericObject
                            .getValue(fullColumnName);
                    if ((longValue != null) && !longValue.equals("0")) {
                        final Double milis = Double.parseDouble(longValue);
                        final Long milliseconds = milis.longValue();
                        content = DateTimeUtils.getDateTime(milliseconds,
                                this.mLabelSpec.getDisplayFormat());
                    } else {
                        final long milliseconds = DateTimeUtils
                                .getCurrentDateTime(true);
                        content = DateTimeUtils.getDateTime(true, milliseconds,
                                this.mLabelSpec.getDisplayFormat());
                    }
                }
                this.mTextView.setText(content == null ? "" : content);
            }
        }
    }

    @Override
    protected void loadDefaultObject() {
        if (!TextUtils.isEmpty(this.mLabelSpec.getContentData())) {
            try {
                loadDatas();
            } catch (final ParseException e) {
                e.printStackTrace();
            }
        } else if (this.mLabelSpec.getDisplayStyle().equals(
                DisplayStyle.CURRENT_DATETIME)) {
            try {
                final long milliseconds = DateTimeUtils
                        .getCurrentDateTime(true);
                final String content = DateTimeUtils.getDateTime(true,
                        milliseconds, this.mLabelSpec.getDisplayFormat());
                this.mTextView.setText(content);
            } catch (final ParseException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void setEventListener() {

    }

    @Override
    public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }

    @Override
    public void setOnClickListener(final OnClickListener onClickListener) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }
}
